package com.fc.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fc.dao.FoodDao;
import com.fc.entities.Food;

/**
 * 
 * @author Ács Ádám
 *
 */
@Repository("foodDao")
public class FoodDaoImpl implements FoodDao {
	private SessionFactory sessionFactory;
	
	@Autowired
	public FoodDaoImpl(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session currentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public void save(Food food) {
		currentSession().save(food);
	}

	@Override
	public void update(Food food) {
		currentSession().update(food);
	}

	@Override
	public void delete(Food food) {
		currentSession().delete(food);
	}

	@Override
	public Food findById(String id) {
		return (Food) currentSession().get(Food.class, id);
	}

	@Override
	public List<Food> findAll() {
 		Query query = currentSession().createQuery("from " + Food.class.getName());
        return query.list();
	}
}
